<?php
defined('SYSPATH') OR die('No direct access allowed.');
/**
 * MAPI COA Project
 * coa.php
 * @author ranuandra
 *
 * 24 Jul 2011
 * tags
 */

class Controller_Coa extends Kohana_Mapi
{
	
	public function action_index()
	{
		$data = ORM::factory('coa')->find_all();
		$view = View::factory('coa/index');
		$view->datas = $data;
		$this->template->container = $view;
	}
	public function action_data($id)
	{
		$data = ORM::factory('productspec')->where('id', '=', $id)->find();
		//print_r($data);
		$view = View::factory('coa/form');
		$view->form = ORM::factory('coa')->getForm(); 
		$view->data = $data;
		$this->template->container = $view;		
	}
	
	public function action_pdf($id)
	{
		$auto_render = false;
		$data = ORM::factory('productspec')->where('id', '=', $id)->find();
		//print_r($data);
		$view = View::factory('coa/pdf');
		$view->data = $data;
		
		include_once Kohana::find_file('vendor', 'tcpdf/config/lang/eng.php', array(), array());		
		include_once Kohana::find_file('vendor', 'tcpdf/tcpdf.php',  array(), array());

		$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

		// set document information
		$pdf->SetCreator(PDF_CREATOR);
		$pdf->SetAuthor('Nicola Asuni');
		$pdf->SetTitle('TCPDF Example 048');
		$pdf->SetSubject('TCPDF Tutorial');
		$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
		
		// set default header data
		$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
		
		// set header and footer fonts
		$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
		$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
		
		// set default monospaced font
		$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
		
		//set margins
		$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
		$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
		$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
		
		//set auto page breaks
		$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
		
		//set image scale factor
		$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
		
		//set some language-dependent strings
		$pdf->setLanguageArray($l);
		
		// ---------------------------------------------------------
		
		// set font
		$pdf->SetFont('helvetica', 'B', 20);
		
		// add a page
		$pdf->AddPage();
		
		$pdf->Write(0, 'Certificate Of Analysis', '', 0, 'C', true, 0, false, false, 0);
		
		$pdf->SetFont('helvetica', '', 8);
		
		
		$pdf->writeHTML($view, true, false, false, false, '');
		
		
		$pdf->Output('coa.pdf', 'I');
		exit;
		// -----------------------------------------------------------------------------
		
		
	}
	
	public function action_new()
	{
		$view = View::factory('productspec/form');
		$view->form = ORM::factory('productspec')->getForm();
		$view->products =ORM::factory('product')->find_all()->as_array('id', 'product_name');
		$view->customers = ORM::factory('customer')->find_all()->as_array('id', 'name'); 
		$this->template->container = $view;		
	}
	
	public function action_save()
	{
		if(isset($_POST))
		{

			try {
				//$post = Validation::factory($_POST);
					$product = ORM::factory('coa');
					$product->create_data($_POST, $product->getField());
					Request::current()->redirect(URL::base().'productspec');

				}
				catch(ORM_Validation_Exception $e)
				{
					$errors = $e->errors('models');
					print_r($errors);
				}
			
		}
		else
		{
			Request::current()->redirect(URL::base().'productspec/new');	
		}		
	}
}